<?php

/**
 * 首页
 * @copyright (c) 2013, Mr.Wang
 * 
 */
class PublicAction extends BaseAction {

    public function login() {
        $refer = base64_decode($_REQUEST['refer']);
        if ($refer == "") {
            $refer = C("siteurl");
        }
        if (session('uid') > 0) {
            $this->success("您已登录,马上为您跳转!", $refer);
            exit;
        }
        cookie("_currentUrl_", $refer);

        //推荐信息
        $roundDZ = _roundDazhe();
        $this->assign('rounddz', $roundDZ);
        $this->display();
    }

    public function reg() {
        if (session('uid') > 0) {
            $this->success("您已登录,马上为您跳转!", $refer);
            exit;
        }

        //推荐信息
        $roundDZ = _roundDazhe();
        $this->assign('rounddz', $roundDZ);
        $this->display();
    }

    public function doReg() {
        $this->model = D("User");
        $model              = $this->model;
        $_POST['is_active'] = ($_POST['type'] == 'shop') ? 0 : 1;
        if (false === $model->create()) {
            $this->error($model->getError());
        }
        //保存当前数据对象
        $list = $model->add();
        if ($list !== false) {
            //添加商户信息
            if ($_POST['type'] == 'shop') {
                $_POST['user_id'] = $list;
                $Shop             = D('Shop');
                if (false === $Shop->create()) {
                    $this->error($model->getError());
                }
                $Shop->add($_POST);
            }
            $this->success('注册成功,感谢您的使用!', U("User/Public/login"));
        } else {
            //失败提示
            $this->error('注册失败!');
        }
    }

    public function doLogin() {
        $where = array(
            "uname" => $_POST['uname']
        );
        $user   = D("User")->where($where)->find();
        if (!$user) {
            $this->error('您尚未注册我们的会员!');
        }
        if ($user['type'] == 'shop' && $user['is_active'] != 1) {
            $this->error('商户会员需后台审核通过才可登录');
        }
        if ($user['is_active'] != 1) {
            $this->error('您的账号尚未激活（商户会员需要经过后台审核）!');
        }
        if (md5($_POST['password']) != $user['password']) {
            $this->error('密码输入错误!');
        }
        session("uid", $user['user_id']);
        session("username", $user['uname']);
        session("user_type", $user['type']);
        if ($user['type'] == "shop") {
            $shop = D("Shop")->from_uid($user['user_id']);
            session("shop_id", $shop['shop_id']);
        } else {
            session("shop_id", 0);
        }
        $this->success('登录成功!', cookie('_currentUrl_'));
    }

    public function loginOut() {
        session(null);
        $this->success('退出成功!', C("SITE_DOMAIN"));
    }

}
